using System.Collections.Generic;

namespace Test.ConsoleProgram.Algorithm.SwordFingerOffer
{
    [TestDescription("剑指 Offer 0009. 用两个栈实现队列")]
    public class No0009_CQueue : AbsBaseTestItem
    {
        /**
        用两个栈实现一个队列。队列的声明如下，请实现它的两个函数 appendTail 和 deleteHead ，分别完成在队列尾部插入整数和在队列头部删除整数的功能。(若队列中没有元素，deleteHead 操作返回 -1 )
        */

        /**
         * Your CQueue object will be instantiated and called as such:
         * CQueue obj = new CQueue();
         * obj.AppendTail(value);
         * int param_2 = obj.DeleteHead();
         */

        public override void OnTest()
        {
            CQueue obj = new CQueue();
            obj.AppendTail(5);
            obj.AppendTail(6);
            obj.AppendTail(23);
            obj.AppendTail(72);
            Assert.TestExe(obj.DeleteHead, 5);
            Assert.TestExe(obj.DeleteHead, 6);
            Assert.TestExe(obj.DeleteHead, 23);
            Assert.TestExe(obj.DeleteHead, 72);
            obj.AppendTail(1);
            obj.AppendTail(5);
            Assert.TestExe(obj.DeleteHead, 1);
            obj.AppendTail(96);
            obj.AppendTail(46);
            Assert.TestExe(obj.DeleteHead, 5);
            Assert.TestExe(obj.DeleteHead, 96);
            Assert.TestExe(obj.DeleteHead, 46);
            Assert.TestExe(obj.DeleteHead, -1);
        }
    }

    public class CQueue
    {
        private Stack<int> stact_in;
        private Stack<int> stact_out;

        public CQueue()
        {
            stact_in = new Stack<int>();
            stact_out = new Stack<int>();
        }

        public void AppendTail(int value)
        {
            stact_in.Push(value);
        }

        public int DeleteHead()
        {
            if (stact_out.Count <= 0)
            {
                while (stact_in.Count > 0)
                {
                    stact_out.Push(stact_in.Pop());
                }
            }
            if (stact_out.Count <= 0)
            {
                return -1;
            }
            return stact_out.Pop();
        }
    }
}
